package com.example.my_first_app;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.*;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;


public class ChartActivity extends Activity {

	ChannelREST channel1;
	
	/** Called when the activity is first created. */
    private XYMultipleSeriesDataset mDataset;
    private XYMultipleSeriesRenderer mRenderer;
    private GraphicalView mChartView;
    private TimeSeries time_series;
    private double[] plot_data;
	private Date[]	plot_date;


    // chart container
    private LinearLayout layout;

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chart);
		setupActionBar();
		// Show the Up button in the action bar.
		
		//get Data from Url
		this.http_request();
		
		//initialize mRenderer
		this.setup_Renderer();
		
	     //Dataset
	     this.createDataset();
	     
	     //setup chart
	     this.setup_Chart();
		
	     layout = (LinearLayout) findViewById(R.id.chart);
	     layout.addView(mChartView);
	}
	

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chart, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	void setup_Renderer()
	{
		mRenderer = new XYMultipleSeriesRenderer();
		mRenderer.setChartTitle("Energy Meter");
		mRenderer.setChartTitleTextSize(30);
		mRenderer.setMargins(new int[] {60, 30, 15, 0});
	    mRenderer.setXTitle("Time");
	    mRenderer.setYTitle("kW");
	    mRenderer.setXLabels(12);
	    mRenderer.setXAxisMin(1367283308000.0);
	    mRenderer.setXAxisMax(1367310900000.0);
	    XYSeriesRenderer r = new XYSeriesRenderer();
	    r.setColor(Color.BLUE);
	    r.setFillBelowLine(true);
	    r.setFillBelowLineColor(Color.BLUE);
	    mRenderer.addSeriesRenderer(r);
	}
	
	void setup_Chart()
	{
		mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer, "hh:mm");
		mChartView.setBackgroundColor(Color.BLACK);
		mChartView.repaint();
	}

	void http_request()
	{
		channel1 = new ChannelREST("http://arun.pv-india.net/AnjarApi/energy_graph");
		channel1.start();
		while (!channel1.isReady()){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		channel1.parseHttpReply();
		
	}
	
	void createDataset() 
	{	
		for(int i=0;i<channel1.getTsLength();i++)
	    {
	    	set_plot_data(channel1.getTs(i), channel1.getValue(i), i);
	    }
		
		time_series = new TimeSeries("series");
			
		for(int i=0;i<channel1.getTsLength();i++)
	    {
	    	time_series.add(plot_date[i], plot_data[i]);
	    }
			
		mDataset = new XYMultipleSeriesDataset();
		mDataset.addSeries(time_series);
		
	}
	
	 void set_plot_data(int timestamp, double value, int index)
	{
			 if(index==0)
			 {
				 plot_data = new double[channel1.getTsLength()];
				 plot_date = new Date[channel1.getTsLength()];
			 }
			Date temp_Date = new Date((long)timestamp*1000);
			plot_date[index] = temp_Date;
			plot_data[index] = value;
		}
	
}
